package com.liu.efk.zdcq.pojo;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created by shi860715@126.com
 * Date 2021/5/8 12:53
 * Description
 */
@Slf4j
public class ClientManager {



    private static final Map<String, Channel> CLIENT_CHANNELS = new ConcurrentHashMap<>();

    private static  final Set<Channel> clients = new CopyOnWriteArraySet<Channel>();


    public static  void bind(String clientName,Channel channel)
    {
        CLIENT_CHANNELS.put(clientName,channel);
    }



    public  static  Channel  getClientChannel(String key)
    {
        return ClientManager.CLIENT_CHANNELS.get(key);
    }

    public static Set<Channel> getClients(){
        return clients;
    }

    public static void addClient(Channel channel){
        clients.add(channel);
    }

    public static void removeClient(Channel channel){
//       连接断开时，更新列表
       for(String key:CLIENT_CHANNELS.keySet()){
           if(getClientChannel(key).id().asLongText().equals(channel.id().asLongText())){
               CLIENT_CHANNELS.remove(key);
           }
       }
        clients.remove(channel);
    }
}
